package com.hopes.bizz.close.gis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import com.hopes.bizz.close.basic.entity.ClosedControlAreaEntity;
import com.hopes.bizz.close.basic.service.ClosedControlAreaService;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.enums.ParkCloseSysDictEnum;
import com.hopes.bizz.close.gis.service.ClosedAreaService;
import com.hopes.bizz.close.gis.vo.AreaControlVO;
import com.hopes.bizz.close.gis.vo.AreaLevelVO;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 封闭化管理（分区管控）
 * @time 2023/10/31 19:50
 */
@Service
@RequiredArgsConstructor
public class ClosedAreaServiceImpl implements ClosedAreaService {

    private final ClosedControlAreaService closedControlAreaService;

    @Override
    public List<AreaLevelVO> queryLevelNum() {
        final List<AreaLevelVO> resList = new ArrayList<>();
        final List<SysDictItem> levelList = DictResolver.getItemsByType(ParkCloseSysDictEnum.close_area_control_level);
        if (CollUtil.isEmpty(levelList)) {
            return resList;
        }
        final Map<Integer, Long> longMap = closedControlAreaService.list().stream().filter(e -> ObjUtil.isNotNull(e.getLevel())).collect(Collectors.groupingBy(ClosedControlAreaEntity::getLevel, Collectors.counting()));
        final Integer sum = longMap.values().stream().mapToInt(Long::intValue).sum();
        for (SysDictItem sysDictItem : levelList) {
            final AreaLevelVO item = new AreaLevelVO();
            item.setLevel(Integer.parseInt(sysDictItem.getCode()));
            item.setNumber(MapUtil.getInt(longMap, Integer.parseInt(sysDictItem.getCode()), 0));
            item.setPercent((int)(NumberUtil.div(item.getNumber(), sum, 2).doubleValue() * 100));

            resList.add(item);
        }
        return resList;
    }

    @Override
    public List<AreaControlVO> queryControlArea() {
        final List<AreaControlVO> resList = new ArrayList<>();
        final Map<Long, List<ClosedControlAreaEntity>> listMap = closedControlAreaService.list().stream().filter(e -> ObjUtil.isNotNull(e.getOrgId())).collect(Collectors.groupingBy(ClosedControlAreaEntity::getOrgId));
        for (Map.Entry<Long, List<ClosedControlAreaEntity>> entry : listMap.entrySet()) {
            final Long key = entry.getKey();
            final Map<Integer, Long> longMap = entry.getValue().stream().filter(e -> ObjUtil.isNotNull(e.getLevel())).collect(Collectors.groupingBy(ClosedControlAreaEntity::getLevel, Collectors.counting()));
            final AreaControlVO item = new AreaControlVO();
            item.setCompanyId(key);
            item.setKeyNum(MapUtil.getInt(longMap, BasicDicConstant.CLOSE_CONTROL_AREA_KEY, 0));
            item.setCoreNum(MapUtil.getInt(longMap, BasicDicConstant.CLOSE_CONTROL_AREA_CORE, 0));
            item.setNormalNum(MapUtil.getInt(longMap, BasicDicConstant.CLOSE_CONTROL_AREA_NORMAL, 0));
            resList.add(item);
        }
        return resList;
    }
}
